import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wallet_utils/report_widget/date_picker/widget/date/brn_date_picker.dart';
import 'package:wallet_utils/report_widget/date_picker/widget/date_range/brn_date_range_picker.dart';
import 'package:wallet_utils/report_widget/date_picker/widget/date_range/brn_date_range_widget.dart';
import 'package:wallet_utils/text_util.dart';

import 'config/brn_picker_config.dart';
import 'config/brn_picker_title_config.dart';
import 'constants/brn_date_picker_constants.dart';

/// @author: pengboboer
/// @createDate: 2021/12/31
/// 通用日期时间选择器
/// 支持：
/// ----------单独选择日期时间
/// ----------选择日期范围

/// 暂时先这样，后续有需要再加

const String ym = "yyyy-MMMM";
const String ymX = "yyyy年,MMMM月";
const String ymd = "yyyy-MMMM-dd";
const String ymdX = "yyyy年,MMMM月,dd日";

const String ymdhmX = "yyyy年,MM月,dd日,HH时:mm分";
// const String ymdhmsX = "yyyy年,MM月,dd日,HH时:mm分:ss秒";
const String hmX = "HH时:mm分";
const String ymdhms = "yyyy-MM-dd-HH-mm-ss";
const String mdhms = "MM-dd-HH-mm-ss";

class DatePickerUtils {
  /// 选择日期时间，默认精确到分，如果需要精确到秒可以修改format
  /// dateTime传入格式为 2020-07-20 18:26:59
  /// pickerTitle为顶部title
  /// format为格式 支持 ym、ymX、ymd、ymdX、ymdhmX、hmX
  /// 确认、改变、弹框消失、取消回调
  /// 示例：
  // void _showDatePicker(BuildContext context) {
  //   DatePickerUtils.showDateTimePicker(context,
  //       maxDateTime: "2021-12-01",
  //       minDateTime: "2014-01-02",
  //       initDateTime: "2018-03-04",
  //       title: "交易时间",
  //       format: ymdX, onConfirm: (dateTime, selectIndexList) {
  //         ToastUtils.showToast(dateTime.toString());
  //       }, onChange: (dateTime, selectIndexList) {
  //         LogUtils.d(dateTime.toString());
  //       }, onClose: () {
  //         LogUtils.d("点击弹框外围消失");
  //       }, onCancel: () {
  //         LogUtils.d("点击取消按钮");
  //       });
  // }
  static void showDateTimePicker(
    BuildContext context, {
    String? maxDateTime,
    String? minDateTime,
    String? initDateTime,
    String? title,
    String? cancelTitle,
    String? confirmTitle,
    String? format,
    int? minuteDivider,
    DateValueCallback? onConfirm,
    DateValueCallback? onChange,
    DateVoidCallback? onClose,
    DateVoidCallback? onCancel,
    bool? canPop
  }) {
    // 处理年月的情况
    if (format == ym || format == ymX) {
      if (!TextUtil.isEmpty(initDateTime)) {
        initDateTime = initDateTime! + "01";
      }
      if (!TextUtil.isEmpty(minDateTime)) {
        minDateTime = minDateTime! + "01";
      }
      if (!TextUtil.isEmpty(maxDateTime)) {
        maxDateTime = maxDateTime! + "01";
      }
    }

    // 处理时分的情况，当format为hmX时，max和minDataTime无效，如有时分的范围需求，后续再加
    if (format == hmX) {
      if (!TextUtil.isEmpty(minDateTime)) {
        minDateTime = "1970-01-01 " + minDateTime!;
      }
      if (!TextUtil.isEmpty(initDateTime)) {
        initDateTime = "1970-01-02 " + initDateTime!;
      }
      if (!TextUtil.isEmpty(maxDateTime)) {
        maxDateTime = "1970-01-03 " + maxDateTime!;
      }
    }

    BrnDatePicker.showDatePicker(context,
        maxDateTime:
            TextUtil.isEmpty(maxDateTime) ? null : DateTime.parse(maxDateTime!),
        minDateTime:
            TextUtil.isEmpty(minDateTime) ? null : DateTime.parse(minDateTime!),
        initialDateTime: TextUtil.isEmpty(initDateTime)
            ? null
            : DateTime.parse(initDateTime!),
        pickerMode: BrnDateTimePickerMode.datetime,
        themeData: PickerConfig.getDefaultPickerConfig(),
        pickerTitleConfig: PickerTitleConfig.getDefaultTitleConfig(
            title: title ?? "", cancelTitle: "取消", confirmTitle: "确定"),
        dateFormat: format ?? ymdhmX,
        minuteDivider: minuteDivider,
        onConfirm: onConfirm,
        onChange: onChange,
        onClose: onClose,
        onCancel: onCancel,
        canPop: canPop);
  }

  /// 选择日期范围
  /// dateTime传入格式为 2020-07-20 18:26:59
  /// pickerTitle为顶部title
  /// format为格式，支持 ym、ymX、ymd、ymdX、
  /// 确认、弹框消失、取消回调
  /// 示例：
  // void _showDateRangePicker(BuildContext context) {
  //   DatePickerUtils.showDateRangePicker(context,
  //       maxDateTime: "2021-12", // 可不传
  //       minDateTime: "2016-01", // 可不传
  //       initialStartDateTime: "2018-03", // 可不传
  //       initialEndDateTime: "2018-03", // 可不传
  //       title: "交易时间",
  //       rangeInitType: RangeInitType.start,
  //       format: ymX, onConfirm: (startDateTime, endDateTime,
  //           startSelectIndexList, endSelectIndexList) {
  //         ToastUtils.showToast(startDateTime.toString());
  //         LogUtils.d(startDateTime.toString() + endDateTime.toString());
  //       }, onClose: () {
  //         LogUtils.d("点击弹框外围消失");
  //       }, onCancel: () {
  //         LogUtils.d("点击取消按钮");
  //       });
  // }
  static void showDateRangePicker(
    BuildContext context, {
    String? maxDateTime,
    String? minDateTime,
    String? initialStartDateTime,
    String? initialEndDateTime,
    String? title,
    String? cancelTitle,
    String? confirmTitle,
    String? format,
    RangeInitType? rangeInitType,
    DateRangeValueCallback? onConfirm,
    DateVoidCallback? onClose,
    DateVoidCallback? onCancel,
    bool? canPop
      }) {
    // 处理年月的情况
    if (format == ym || format == ymX) {
      if (!TextUtil.isEmpty(initialStartDateTime)) {
        initialStartDateTime = initialStartDateTime! + "01";
      }
      if (!TextUtil.isEmpty(initialStartDateTime)) {
        initialEndDateTime = initialEndDateTime! + "01";
      }
      if (!TextUtil.isEmpty(minDateTime)) {
        minDateTime = minDateTime! + "01";
      }
      if (!TextUtil.isEmpty(maxDateTime)) {
        maxDateTime = maxDateTime! + "01";
      }
    }

    BrnDateRangePicker.showDatePicker(context,
        maxDateTime:
            TextUtil.isEmpty(maxDateTime) ? null : DateTime.parse(maxDateTime!),
        minDateTime:
            TextUtil.isEmpty(minDateTime) ? null : DateTime.parse(minDateTime!),
        initialStartDateTime: TextUtil.isEmpty(initialStartDateTime)
            ? null
            : DateTime.parse(initialStartDateTime!),
        initialEndDateTime: TextUtil.isEmpty(initialEndDateTime)
            ? null
            : DateTime.parse(initialEndDateTime!),
        pickerMode: BrnDateTimeRangePickerMode.date,
        themeData: PickerConfig.getDefaultPickerConfig(pickerHeight: 252.w),
        pickerTitleConfig: PickerTitleConfig.getDefaultTitleConfig(
            title: title ?? "", cancelTitle: "取消", confirmTitle: "确定"),
        dateFormat: format ?? ymdX,
        rangeInitType: rangeInitType,
        onConfirm: onConfirm,
        onClose: onClose,
        onCancel: onCancel,
        canPop: canPop);
  }

  static String getStringDateByTimeStamp(int? timeStamp,
      {String format = ymdhms}) {
    if (timeStamp == null) return "";
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timeStamp);
    return getConfirmValue(dateTime, format);
  }

  /// 因为日期返回值都为DateTime对象，
  /// 此方法可对DateTime做一个简单处理
  /// 也可自行处理，比较灵活
  static String getConfirmValue(DateTime dateTime, String format) {
    String y = _fourDigits(dateTime.year);
    String m = _twoDigits(dateTime.month);
    String d = _twoDigits(dateTime.day);
    String h = _twoDigits(dateTime.hour);
    String min = _twoDigits(dateTime.minute);
    String s = _twoDigits(dateTime.second);
    if (format == ym || format == ymX) {
      return "$y-$m";
    }

    if (format == ymd || format == ymdX) {
      return "$y-$m-$d";
    }

    if (format == ymdhmX) {
      return "$y-$m-$d $h:$min";
    }

    if (format == hmX) {
      return "$h:$m";
    }

    if (format == ymdhms) {
      return "$y-$m-$d $h:$min:$s";
    }

    if (format == mdhms) {
      return "$m-$d $h:$min:$s";
    }

    return dateTime.toString();
  }

  static String _fourDigits(int n) {
    int absN = n.abs();
    String sign = n < 0 ? "-" : "";
    if (absN >= 1000) return "$n";
    if (absN >= 100) return "${sign}0$absN";
    if (absN >= 10) return "${sign}00$absN";
    return "${sign}000$absN";
  }

  static String _twoDigits(int n) {
    if (n >= 10) return "${n}";
    return "0${n}";
  }
}
